//logs.js
//获取应用实例
const app = getApp()

Page({
  data: {
    vip_on: 1, //vip套餐选中
    activity_on: 0, //活动套餐选中
    package_type: 'vip', //选择vip套餐类型 vip，activity 活动套餐
    phone: '',
    vipType: '',
    dueTime: '',
    payBut: "立即开通VIP",
    explainInfo: [],
    paySatus: false,//支付状态
    vipInfo: [
      // {
      //   vipId: '1',
      //   vipname: '连续包月',
      //   vipPrice: '25',
      //   vipRemke: '新用户首月30元'
      // },{
      //   vipId: '2',
      //   vipname: '连续包季',
      //   vipPrice: '99',
      //   vipRemke: '折合 ￥33/月'
      // },{
      //   vipId: '3',
      //   vipname: '连续包年',
      //   vipPrice: '255',
      //   vipRemke: '折合 ￥33/月'
      // }
    ],
    activityList: [
      // {
      //   activityId: '1',
      //   activityName: '会展活动/半年',
      //   activityPrice: '99',
      // }
    ],
    type: '1' //跳转页面类型 1为加入会员 2为续会员
  },
  onShow: function () {
    var that = this
    // 获取vip套餐信息以及用户信息
    
    var wxUserId = app.globalData.wxUserId
    app.http.post('/wechat/getVipPayInfo',{userId: wxUserId}).then(res =>{
      var type = 1
      if(res.status == '200'){
        let dueTime = "已"
        let payBut = "立即开通VIP"
        var result = res.data
        var userInfo = result.userInfo
        // 根据用户类别判断是否是加入会员 还是显示会员信息
        var wxUserType = userInfo.userType
        app.globalData.wxUserType = wxUserType //更新用户权限信息
        if (wxUserType == '3') {
          type = 2
          let days = that.judgeDateDays(userInfo.dueTime)
          if (days > 0) { //会员未过期
           if(days < 30){
            dueTime =  days + "天后"
           } else {
            dueTime =  userInfo.dueTime
           }
           payBut = "立即续费VIP"
          }
        }
        that.setData({
          type: type,
          vipInfo: result.vipInfo,
          activityList: result.activityInfo,
          explainInfo: result.explainInfo,
          phone: userInfo.phone,
          vipType: userInfo.vipType,
          payBut: payBut,
          dueTime: dueTime
        })
      }
    })

  },
  login: function() {
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },
  onLoad: function (option) {
    
  },
  chooseVip: function(e) { // vip套餐选择事件
    var that = this
    var type = e.currentTarget.dataset.vip
    that.setData({
      package_type: 'vip',
      vip_on: type
    });
  },
  chooseActivity: function(e) { //活动套餐点击事件
    var that = this
    var activity = e.currentTarget.dataset.activity
    that.setData({
      package_type: 'activity',
      activity_on: activity
    });
  },
  judgeDateDays(dateTime){//判断日期与当前日期的天数差
    // 指定日期和时间
    var EndTime = new Date(dateTime);
    // 当前系统时间
    var NowTime = new Date();
    var t = EndTime.getTime() - NowTime.getTime();
    var d = Math.floor(t / 1000 / 60 / 60 / 24);
    return d;
  },
  payVip: function() {
    var that = this
    if(!that.data.paySatus){
      var wxUserId = app.globalData.wxUserId
      var url = ""
      var pay = ""
      var ptype = that.data.package_type
      if(ptype === 'vip') {
        url = '/wechat/vipWxPay'
        pay = that.data.vip_on
      } else {
        url = '/wechat/activityWxPay'
        pay = that.data.activity_on
      }
      // 设备平台判断
      if(app.globalData.platform == "ios" && !app.globalData.iosPayStatus){
        wx.showToast({
          title: '由于相关规范，iOS功能暂不可用',
          icon: 'none',
          duration: 3000
        })
      }else {
        wx.showLoading()   
        that.payStartStatus()
        var wxUserId = app.globalData.wxUserId
        var session = app.globalData.session
        var data = {
          userId: wxUserId,
          openId: session.openid,
          goodsCodeValue: pay
        }
        app.http.post(url ,data).then(res => {
          setTimeout(function () {
            wx.hideLoading()
            that.payEndStatus()
          },1 * 1000);
          if(res.status == '200'){
            var info = res.data
            that.doWxPay(info.singlePay, info.orderNo)
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none"
            })
          }
        })
      }    
    }
  },
  doWxPay(param, orderNo) { //拉起微信支付组件
    var that = this
    //小程序发起微信支付
    wx.requestPayment({
      timeStamp: param.timeStamp,//记住，这边的timeStamp一定要是字符串类型的，不然会报错
      nonceStr: param.nonce_str,
      package: param.packageStr,
      signType: param.signType,
      paySign: param.sign,
      success: function (event) {
        // 定时查询后台 是否支付完成。
        wx.showLoading()      
        that.getPayStatus(orderNo, 1)
      },
      fail: function (error) {
        // fail
        wx.navigateTo({
          url: '/pages/payResult/payResult?type=2&pageType=2&orderNo=' + orderNo
        })
      }
    })
  },
  getPayStatus: function(orderNo, num) { // 获取微信支付状态
    var that = this
    console.log(orderNo + '---------' + num)
    if(num < 10) {
      num++
      app.http.get('/wechat/getOrderStatus?orderNo='+ orderNo).then(res => {
        if(res.status == '200'){
          if(res.data) {
            wx.hideLoading()
            wx.navigateTo({
              url: '/pages/payResult/payResult?type=1&pageType=2&orderNo=' + orderNo
            })
            // wx.showToast({
            //   title: '支付成功',
            //   icon: 'success',
            //   duration: 2000
            // });
            // setTimeout(() => {
              
            // }, 2000);   
          } else {
            setTimeout(that.getPayStatus(orderNo, num), 2000);
          }
        } else { // 再次请求
          setTimeout(that.getPayStatus(orderNo, num), 2000);
        }
      })
    } else {
      wx.hideLoading()
      wx.navigateTo({
        url: '/pages/payResult/payResult?type=2&pageType=2&orderNo=' + orderNo
      })
    }
  },
  payStartStatus:function() {
    var that = this
    that.setData({
      paySatus: true
    });
  },
  payEndStatus: function() {
    var that = this
    that.setData({
      paySatus: false
    });
  },

})
